﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Gift.WebApp;
using Gift.Model;


public partial class admin_manager_managerRole : BaseAdmin
    {

     

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.ManagerRole);
                this.BuildRuleList();
            }
        }

        private void BuildRuleList()
        {
            StringBuilder strBody = new StringBuilder();
            StringBuilder strTable = new StringBuilder();
            StringBuilder strCheckedAdmin = new StringBuilder(",");
            DataSet ds = new DataSet();
            string sAdminID = Request.QueryString["id"];
            ds = BaseFactory.theRole.GetList(string.Empty);
            strTable.Append("<table class='tableRule' border=\"1\" cellpadding=\"3\" cellspacing=\"1\" bordercolor=\"#E2E7EB\">");
            if (ds.Tables.Count > 0)
            {
                int iRowIndex = 0;
              
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (iRowIndex % 5 == 0)
                    {
                        strBody.Append("<tr>");
                    }
                    strBody.Append("<td valign='top'>");
                 
                        string sChecked = "";
                        if (BaseFactory.theRole.GetRoleByRoleIdAndAdminId(int.Parse(dr["id"].ToString()), int.Parse(sAdminID)))
                        {
                            sChecked = "checked='checked'";
                            strCheckedAdmin.Append(dr["id"].ToString() + ",");
                        }
                        strBody.Append("<input type='checkbox' onclick='setAdminId(this.id)' " + sChecked + " id='" + dr["id"].ToString() + "'/>" + dr["RoleName"].ToString());
                 
                    strBody.Append("</td>");
                    if (iRowIndex != 0 && iRowIndex % 4 == 0)
                    {
                        strBody.Append("</tr>");
                    }
                    iRowIndex++;
                }
                if (iRowIndex % 5 != 0)
                {
                    if (iRowIndex % 5 == 1)
                    {
                        strBody.Append("<td></td><td></td><td></td><td></td>");
                    }
                    else  if (iRowIndex % 5 == 2)
                    {
                        strBody.Append("<td></td><td></td><td></td>");
                    }
                    else  if (iRowIndex % 5 == 3)
                    {
                        strBody.Append("<td></td><td></td>");
                    }
                    else    if (iRowIndex % 5 == 4)
                    {
                        strBody.Append("<td></td>");
                    }
                    strBody.Append("</tr>");
                }
            }

            strTable.Append(strBody.ToString());
            strTable.Append("</table>");

            this.ltRuleList.Text = strTable.ToString();
            this.hdList.Value = strCheckedAdmin.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sRoleIds = string.Empty;
            if (this.hdList.Value.Length > 1)
            {
                sRoleIds = this.hdList.Value.Substring(1, this.hdList.Value.Length - 2);
            }
            else
            {
                sRoleIds = "0,0";
            }

            BaseFactory.theRole.SaveAdminRole(int.Parse(Request.QueryString["id"]), sRoleIds);
            Response.Write("<script>alert('保存成功！');window.location.href='ManagerList.aspx';</script>");
        }
    }
